import { Window_Selectable } from "@/windows/Window_Selectable.js";
import { ColorExtraction } from "@/utils/ColorExtraction.js";

export class Window_SkillList extends Window_Selectable {
    constructor(width,height) {
        super(width, height,false);
        this.init(width,height);
    }

    init(width, height) {
        this._actor = null;
        this._stypeId = 0;
        this._list = [];
        this._numWidth=null;
        this.okHandler=null;
        this.cancelHandler=null;
        this.changedHandler=null;
        super.init(width,height);
    }

    destroy(opts){
        super.destroy(opts);
        this._actor = null;
        this._list.length=0;
        this.okHandler=null;
        this.cancelHandler=null;
        this.changedHandler=null;
    }

    setHandler(name,callback){
        if(name=='ok'){
            this.okHandler=callback;
        }else if(name=='cancel'){
            this.cancelHandler=callback;
        }else if(name=='changed'){
            this.changedHandler=callback;
        }
    }

    onResponse(index){
        this.okHandler?.(this._list[index],index);
        this.refreshItem(index);
    }

    onSelectChanged(index,oldIndex){
        this.changedHandler?.(this._list[index],index,this._list[oldIndex],oldIndex);
    }

    onCancel(){
        this.cancelHandler?.();
    }

    setActor(actor) {
        if (this._actor !== actor) {
            this._actor = actor;
            this.refresh();
        }
    }

    setStypeId(stypeId) {
        if (this._stypeId != stypeId) {
            this._stypeId = stypeId;
            if(this._stypeId<=0||this._stypeId==null){
                this.contents.clear();
                return;
            }
            this.makeItemList();
            this.refresh();
        }
    }

    column(){return 2;}
    viewColumn(){return 2;}
    row(){return this.length();}
    viewRow(){return Math.ceil(this.contentsHeight()/this.itemHeight());}
    length(){return this._list.length;}
    itemWidth(){return this.contentsWidth()/this.viewColumn();}
    itemEnabled(index) {return super.itemEnabled(index)&&this._actor?.canUse(this._list[index]);}

    makeItemList() {
        if (this._actor) {
            this._list = this._actor.skills().filter(skill=>skill?.stypeId==this._stypeId);
        }else{
            this._list.length=0;
        }
    }

    drawItem(index,x,y,width,height) {
        let skill = this._list[index];
        if (skill) {
            let costWidth = this.costWidth();
            let enabled=this.itemEnabled(index);
            if(!enabled){
                this.changePaintOpacity(false);
            }
            this.drawItemName(skill, x, y, width - costWidth);
            this.drawSkillCost(skill, x+width-costWidth, y, costWidth,height);
            if(!enabled){
                this.changePaintOpacity(true);
            }
        }
    }

    costWidth() {
        if(!this._numWidth){
            this._numWidth=this.textWidth('000');
        }
        return this._numWidth;
    }

    drawSkillCost(skill, x, y, width,height) {
        if (this._actor.skillTpCost(skill) > 0) {
            this.changeTextColor(ColorExtraction.tpCostColor());
            this.drawText(this._actor.skillTpCost(skill), x, y, width,height);
        } else if (this._actor.skillMpCost(skill) > 0) {
            this.changeTextColor(ColorExtraction.mpCostColor());
            this.drawText(this._actor.skillMpCost(skill), x, y, width,height);
        }
    }
}